package com.hson.demo.base.remote.hessian;

import org.springframework.beans.BeansException;  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.BeanFactoryAware;  
import org.springframework.beans.factory.BeanNameAware;  
import org.springframework.beans.factory.DisposableBean;  
import org.springframework.beans.factory.InitializingBean;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.ApplicationContextAware;  
  
/**
 * 在ApplicationContext容器中的bean的生命周期详解
 * @author:chenhs E-mail:hson08_chen@163.com
 * Created on 2017年1月19日 下午4:13:17
 */
public class Car implements BeanNameAware, ApplicationContextAware, BeanFactoryAware, InitializingBean, DisposableBean {  
    private String brand;  
    private String color;  
    private int maxSpeed;  
      
    private BeanFactory beanFactory;  
    private String beanName;  
    private ApplicationContext context;  
      
    public Car() {  
        System.out.println("调用Car()构造函数......");  
    }  
      
    public String getColor() {  
        return color;  
    }  
  
    public void setColor(String color) {  
        System.out.println("调用setColor()设置属性......");  
        this.color = color;  
    }  
  
    public int getMaxSpeed() {  
        return maxSpeed;  
    }  
  
    public void setMaxSpeed(int maxSpeed) {  
        System.out.println("调用setMaxSpeed()设置属性......");  
        this.maxSpeed = maxSpeed;  
    }  
  
    public String getBrand() {  
        return brand;  
    }  
      
    public void setBrand(String brand) {  
        System.out.println("调用setBrand()设置属性......");  
        this.brand = brand;  
    }  
      
    public void introduce() {  
        System.out.println("brand:  "+ brand +"; color: "+ color +"; maxSpeed: " + maxSpeed);  
    }  
  
    public void setBeanName(String beanName) {  
        System.out.println("调用BeanNameAware.setBeanName()");  
        this.beanName = beanName;  
    }  
  
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
        System.out.println("调用BeanFactoryAware.setBeanFactory()");  
        this.beanFactory = beanFactory;  
        
    }  
      
    public void setApplicationContext(ApplicationContext context) throws BeansException {  
        System.out.println("调用ApplicationContextAware.setApplicationContext()");  
        this.context = context;  
    }  
      
    public void myInit() {  
        System.out.println("调用init-method所指定的myInit()，将maxSpeed设置为240.");  
        this.maxSpeed = 240;  
    }  
  
    public void afterPropertiesSet() throws Exception {  
        System.out.println("调用InitializingBean.afterPropertiesSet()。");  
        Car c = (Car)this.context.getBean("car");
        c.setColor("Red");
    }  
      
    public void myDestroy() {  
        System.out.println("调用destroy-method所指定的myDestroy().");  
    }  
  
    public void destroy() throws Exception {  
        System.out.println("调用DisposableBean.destroy()。");  
    }  
  
}  
